#!/usr/bin/perl
use strict;

# Possible environment variables provided by nautilus
# NAUTILUS_SCRIPT_SELECTED_FILE_PATHS: newline-delimited paths for selected files (only if local)
# NAUTILUS_SCRIPT_SELECTED_URIS: newline-delimited URIs for selected files
# NAUTILUS_SCRIPT_CURRENT_URI: current location
# NAUTILUS_SCRIPT_WINDOW_GEOMETRY: position and size of current window

# First, lets provide a display "default".
my $display = $ENV{'DISPLAY'};
if ( $display ) {
    $display =~ s/\://;
}

# Or get it from the filename of the script
my ( $testdisplay ) = $0 =~ /.*(\d\.\d)$/;
$display = $testdisplay if $testdisplay;

# Now, lets check for Nautilus funtime ENV variables...

my $path = $ENV{'NAUTILUS_SCRIPT_CURRENT_URI'};     # Chosen Path
$path =~ s/file\:\/\///g;                           # Remove file:/// nonsense

my $photo = pop( @ARGV ); # More than one selected, too bad!
my $pathphoto =  $path . "/" . $photo;

# Properly handle files with spaces, by inserting a literal \ in front of
# whitechars...
$path =~ s/\s/\\ /g;
$photo =~ s/\s/\\ /g;

# Handle files and directories
if ( -d $pathphoto ) {
    system( "wallpaper --photodir:" . $display . ":" . $pathphoto );
} elsif ( -f $pathphoto ) {
    system( "wallpaper --photo:" . $display . ":" . $pathphoto );
} 
